﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Coolzon.Common
{
    public class SerializeUtility
    {
        public static byte[] SerializeToBinary(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(ms, obj);

                byte[] resbytes = new byte[ms.Length];

                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(resbytes, 0, (int)ms.Length);

                return resbytes;
            }
        }

        public static object DeserializeFromBinary(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                return binaryFormatter.Deserialize(ms);
            }
        }
    }
}
